// pagesShouYe/pages/xiangCun/index.js
const app = getApp();
import { getStorage } from "../../../utils/localStorage";
import { baseUrl } from "../../../api/request";
import { listVillage, listHotSpotsMore_townShip } from "../../../api/homePage"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        //自定义导航栏组件参数
        navData: {
            level: 2,
            title: '乡镇景点'
        },
        //自定义导航栏高度
        pageTop: app.globalData.navHeight + app.globalData.navTop,
        //图片ip地址
        imgUrl: undefined,
        //乡村列表
        list: [],
        //当前位置经纬度
        latLng: undefined,
        //列表查询参数
        listQuery: {
            pageSize: 10,
            pageNum: 1,
            longitude: "",
            latitude: "",
            townShipName: "",
            browseNumber: "",
            dictValue: "",
            distance: ""
        },
        //列表总页数
        total: 0,
        //上拉加载更多状态
        showPull: true,
        //景区类型搜索状态
        jqSearchValue: "景区类型",
        jqSearchFlag: false,
        jqSearchIndex: undefined,
        //距离位置搜索状态
        jlSearchValue: "距离位置",
        jlSearchFlag: false,
        jlSearchIndex: undefined,
        //浏览数量搜索状态
        lnSearchValue: "浏览数量",
        lnSearchFlag: false,
        lnSearchIndex: undefined,
        //景区类型列表
        scenicSpottype: [],
        //距离位置
        distancePosition: [
            {
                label: '不限',
                value: ''
            },
            {
                label: '2km',
                value: '2'
            },
            {
                label: '5km',
                value: '5'
            },
            {
                label: '10km',
                value: '10'
            },
            {
                label: '20km',
                value: '20'
            },
            {
                label: '50km',
                value: '50'
            },
            {
                label: '100km',
                value: '100'
            },
        ],
        //浏览数量
        viewNumber: [
            {
                label: '不限',
                value: ''
            },
            {
                label: '10条以上',
                value: '10'
            },
            {
                label: '50条以上',
                value: '50'
            },
            {
                label: '500条以上',
                value: '500'
            }
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            imgUrl: baseUrl,
            "listQuery.longitude": getStorage("latLng").lng,
            "listQuery.latitude": getStorage("latLng").lat,
            "listQuery.townShipName": options.townName,
        });
        this.getList();

        //获取景区类型字典
        app.getDict("landscape_classify").then(res => {
            res.data.unshift({ dictLabel: '不限' });
            this.setData({
                scenicSpottype: res.data
            });
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    //获取列表
    getList() {
        const _this = this.data;
        listHotSpotsMore_townShip(_this.listQuery).then(res => {
            res.data.data.list.forEach(item => {
                item.imageScale = item.imageScale ? item.imageScale.split(",") : [];
                item.landscapeClassify = item.landscapeClassify ? item.landscapeClassify.split(",") : [];
            });
            this.setData({
                list: [..._this.list, ...res.data.data.list],
                total: Math.ceil(res.data.data.total / _this.listQuery.pageSize)
            });
        });
    },


    /* 搜索框切换点击事件 */
    searchNavTap(e) {
        const type = e.currentTarget.dataset.type,
            flag = e.currentTarget.dataset.flag;
        switch (type) {
            case "景区":
                this.setData({
                    jqSearchFlag: !flag,
                    jlSearchFlag: false,
                    lnSearchFlag: false
                });
                break;
            case "距离":
                this.setData({
                    jqSearchFlag: false,
                    jlSearchFlag: !flag,
                    lnSearchFlag: false
                });
                break;
            case "浏览":
                this.setData({
                    jqSearchFlag: false,
                    jlSearchFlag: false,
                    lnSearchFlag: !flag
                });
                break;
        };
    },
    lineNavTapzk(e) {
        const type = e.currentTarget.dataset.type,
            index = e.currentTarget.dataset.index,
            _this = this.data;
        this.setData({
            list: [],
            "listQuery.pageNum": 1,
        });
        switch (type) {
            case "景区":
                this.setData({
                    jqSearchIndex: index,
                    "listQuery.dictValue": _this.scenicSpottype[index].dictLabel === "不限" ? "" : _this.scenicSpottype[index].dictValue,
                    jqSearchValue: _this.scenicSpottype[index].dictLabel,
                    jqSearchFlag: false,
                });
                break;
            case "距离":
                this.setData({
                    jlSearchIndex: index,
                    "listQuery.distance": _this.distancePosition[index].value,
                    jlSearchValue: _this.distancePosition[index].label,
                    jlSearchFlag: false
                });
                break;
            case "浏览":
                this.setData({
                    lnSearchIndex: index,
                    "listQuery.browseNumber": _this.viewNumber[index].value,
                    lnSearchValue: _this.viewNumber[index].label,
                    lnSearchFlag: false
                });
                break;
        };
        this.getList();
    },

    //列表点击事件
    listTap(e) {
        const item = e.currentTarget.dataset.item;
        wx.navigateTo({
          url: '/pagesShouYe/pages/jingquDetail/index?id='+item.landscapesId,
        });
    },

    //图片点击事件
    imgTap(e) {
        const url = e.currentTarget.dataset.url,
            urlList = e.currentTarget.dataset.urllist;
        console.log(urlList, 'urlList');
        const arr = urlList.map(item => this.data.imgUrl + item);
        app.imagePreview(url, arr);
    },

    //上拉加载更多
    pullUp() {
        let _this = this.data;
        if ((_this.listQuery.pageNum + 1) <= _this.total) {
            this.setData({
                showPull: false,
                "listQuery.pageNum": _this.listQuery.pageNum += 1
            });
            this.getList();
        } else {
            this.setData({
                showPull: true
            });
        };
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        console.log(123123);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})